package 泛型;

class TestContainter1 {
    void printContainer(Containter1<?> c) {
        System.out.println(c.get());
    }
}

class person {
    private String name;

    public person(String name) {
        this.name = name;

    }
}

public class Containter1<T> { //定义泛型类
    private T elem;

    public Containter1(T elem) {
        this.elem = elem;
    }

    public T get() {
        return elem;
    }

    public T set(T elem) {
        return this.elem = elem;
    }


    public static void main(String[] args) {
        //创建泛型的对象，泛型的类型参数用Student类代替
        // TODO Auto-generated method stub
        Containter1<person> c1 = new Containter1<person>(new person("Ann"));
		System.out.println(c1.get());
		person a=new person("Bill");
		person s1=c1.get();  //不发生类型转换
	    c1.set(a); //发生编译错误
		System.out.println(c1.get());
		System.out.println(c1.getClass());
        TestContainter1 mytest = new TestContainter1();
        mytest.printContainer(c1);//
        Containter1<Integer> c2 = new Containter1<Integer>(new Integer(100));
        TestContainter1 mytest2 = new TestContainter1();
        mytest2.printContainer(c2);
        Containter1<?> c3 = new Containter1<String>("test");
        System.out.println(c3.get());
        // c3.set("result");//由于不知道c1容器中能容纳的元素，无法向其中添加对象，改为null则没有错误，因为null是所有类型的的成员
        /**注意
         * @1对于通过将类型参数设为通配副？的泛型对象，只能读取其中的元素，不能向其中添加对象。
         * @2通配符只能通过引用声明使用，使用new创建对象时，不能使用通配符。
         * 例如：Container1 <?>c1=new Container1<?>();是错误的
         */


    }

}
